package com.example.filetransfer.publicsource.fileutils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


public class XmlSaver {
    private Document document;
    private String saveFileName;

    public XmlSaver(String saveFileName) {
        init();
        this.saveFileName = saveFileName;
    }

    public void init() {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            this.document = builder.newDocument();
        } catch (ParserConfigurationException e) {
            System.out.println(e.getMessage());
        }
    }
    
    private void saveDir(Element parent, Dir dir) {
        Element currentElement = createDirElement(dir); 
        if (parent == null) {
            parent = currentElement;
            document.appendChild(parent);
        }
        for (Dir childDir : dir.getChildDirs()) {
            Element dirElement = createDirElement(childDir);
            parent.appendChild(dirElement);
            saveDir(dirElement, childDir);
        }
        for (MyFile childFile : dir.getChildFiles()) {
            Element fileElement = createFileElement(childFile);
            parent.appendChild(fileElement);
        }
    }

    public void saveDir(Dir dir) {
        saveDir(null, dir);
        TransformerFactory tf = TransformerFactory.newInstance();
        try {
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(document);
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            PrintWriter pw = new PrintWriter(new FileOutputStream(saveFileName));
            StreamResult result = new StreamResult(pw);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            System.out.println(e.getMessage());
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (TransformerException e) {
            System.out.println(e.getMessage());
        }
    }

    private Element createDirElement(Dir dir) {
        Element element = document.createElement("dir");
        element.setAttribute("path", dir.dirName());
        return element;
    }

    private Element createFileElement(MyFile childFile) {
        Element element = document.createElement("file");
        element.setAttribute("path", childFile.getFilePath());
        long modifyTime = Long.parseLong(childFile.getLastChanged());
        element.setAttribute("lastModify", modifyTime+"");
        return element;
    }
}
